home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Temp / MiscTimeAdditions / Time_Stuff / Time.m < prev    next >
Encoding:
Text File  |  1995-04-12  |  8.8 KB  |  563 lines

  1. // Copyright (C) 1995
  2. // Use is governed by the MiscKit license
  3.  
  4. #import "Time.h"
  5. #import <string.h>
  6. #import <sys/time.h>
  7.  
  8. #define SIZE_OF_TIME_STRING 27
  9. #define SIZE_OF_MNAME 10
  10. #define SIZE_OF_MABRNAME 4
  11. #define SIZE_OF_DNAME 10
  12. #define SIZE_OF_DABRNAME 4
  13.  
  14. @implementation Time
  15.  
  16. //--------------------------------
  17.  
  18. - free
  19. {
  20.     int i;
  21.     if( timeString )
  22.         NXZoneFree([ self zone ], timeString );
  23.     if( timesPtr )
  24.         NXZoneFree( [self zone], timesPtr );
  25.     for(i=0;i<12;i++){
  26.         NXZoneFree( [self zone], mNames[i] );
  27.         NXZoneFree( [self zone], mAbrNames[i] );
  28.     }
  29.     for(i=0;i<7;i++){
  30.         NXZoneFree( [self zone], dNames[i] );
  31.         NXZoneFree( [self zone], dAbrNames[i] );
  32.     }
  33.     return [super free];
  34. }
  35.  
  36. - init
  37. {
  38.     [super init];
  39.     timesPtr = NULL;
  40.     myTime = 0;
  41.     [self _syncTimesStruct];
  42.     [self initNames];
  43.     return self;
  44. }
  45.  
  46. - initWithCurrentTime
  47. {
  48.     [self init];
  49.     [self setToCurrentTime];
  50.     return self;
  51. }
  52.  
  53. - initNames
  54. {
  55.     int i;
  56.     for(i=0;i<12;i++){
  57.         mNames[i] = NXZoneMalloc( [self zone], SIZE_OF_MNAME * 12 );
  58.         mAbrNames[i] = NXZoneMalloc( [self zone], SIZE_OF_MABRNAME * 12 );
  59.     }
  60.  
  61.     for(i=0;i<7;i++){
  62.         dNames[i] = NXZoneMalloc( [self zone], SIZE_OF_DNAME * 7 );
  63.         dAbrNames[i] = NXZoneMalloc( [self zone], SIZE_OF_DABRNAME * 7 );
  64.     }
  65.  
  66.     mNames[0] =    "January";    mAbrNames[0] =    "Jan";
  67.     mNames[1] =    "Febuary";    mAbrNames[1] =    "Feb";
  68.     mNames[2] =    "March";    mAbrNames[2] =    "Mar";
  69.     mNames[3] =    "April";    mAbrNames[3] =    "Apr";
  70.     mNames[4] =    "May";        mAbrNames[4] =    "May";
  71.     mNames[5] =    "June";        mAbrNames[5] =    "Jun";
  72.     mNames[6] =    "July";        mAbrNames[6] =    "Jul";
  73.     mNames[7] =    "August";    mAbrNames[7] =    "Aug";
  74.     mNames[8] =    "September";    mAbrNames[8] =    "Sep";
  75.     mNames[9] =    "October";    mAbrNames[9] =    "Oct";
  76.     mNames[10] =    "November";    mAbrNames[10] =    "Nov";
  77.     mNames[11] =    "December";    mAbrNames[11] =    "Dec";
  78.  
  79.     dNames[0] =    "Sunday";    dAbrNames[0] =     "Sun";
  80.     dNames[1] =    "Monday";    dAbrNames[1] =     "Mon";
  81.     dNames[2] =    "Tuesday";    dAbrNames[2] =     "Tue";
  82.     dNames[3] =    "Wednesday";    dAbrNames[3] =     "Wed";
  83.     dNames[4] =    "Thursday";    dAbrNames[4] =     "Thu";
  84.     dNames[5] =    "Friday";    dAbrNames[5] =     "Fri";
  85.     dNames[6] =    "Saturday";    dAbrNames[6] =     "Sat";
  86.  
  87.     return self;
  88. }
  89.  
  90. //--------------------------------
  91.  
  92. - (BOOL) abrMode
  93. {
  94.     return theAbrMode;
  95. }
  96.  
  97. - (BOOL) setAbrMode: (BOOL)mode
  98. {
  99.     return (theAbrMode = mode);
  100. }
  101. - (long) indexOfDayName: (const char *)dayName
  102. {
  103.     [self notImplemented:_cmd];
  104.     return 0;
  105. }
  106.  
  107. - (const char *) nameOfDay: (long)dayIndex
  108. {
  109.     if((dayIndex>=1)&&(dayIndex<=7)){
  110.         if([self abrMode])
  111.             return dAbrNames[dayIndex-1];
  112.         return dNames[dayIndex-1];
  113.     }
  114.     return NULL;
  115. }
  116.  
  117. - (const char *) nameOfDay
  118. {
  119.     return [self nameOfDay:[self dayOfWeek]];
  120. }
  121.  
  122. - (long) indexOfMonthName: (const char *)monthName
  123. {
  124.     [self notImplemented:_cmd];
  125.     return 0;
  126. }
  127.  
  128. - (const char *) nameOfMonth: (long)monthIndex
  129. {
  130.     if((monthIndex>=1)&&(monthIndex<=12)){
  131.         if([self abrMode])
  132.             return mAbrNames[monthIndex-1];
  133.         return mNames[monthIndex-1];
  134.     }
  135.     return mNames[monthIndex];
  136. }
  137.  
  138. - (const char *) nameOfMonth
  139. {
  140.     return [self nameOfMonth:[self month]];
  141. }
  142.  
  143. - (const char *) timeString
  144. {
  145.     char *tmpPtr;
  146.     if( !timeString )
  147.         timeString = NXZoneMalloc( [self zone], SIZE_OF_TIME_STRING );
  148.     tmpPtr = ctime( &myTime );
  149.     bcopy( tmpPtr, timeString, strlen( tmpPtr ));
  150.     return timeString;
  151. }
  152.  
  153. - (const char *) timeZone
  154. {
  155.     if( timesPtr )
  156.         return timesPtr->tm_zone;
  157.     return NULL;
  158. }
  159.  
  160. //--------------------------------
  161.  
  162. - (const time_t *)getTime_t
  163. {
  164.     return &myTime;
  165. }
  166.  
  167. - (BOOL) leapYear: (long)year
  168. {
  169.     return (((year % 4) == 0) && ((year % 400) != 0))?YES:NO;
  170. }
  171.  
  172. - (BOOL) leapYear
  173. {
  174.     return([self leapYear:[self year]]);
  175. }
  176.  
  177. - (long) daysInYear: (long)year
  178. {
  179.     if([self leapYear])
  180.         return 366;
  181.     return 355;
  182. }
  183.  
  184. - (long) daysInYear
  185. {
  186.     return [self daysInYear:[self year]];
  187. }
  188.  
  189. - (long) daysInMonth: (long)monthIndex forYear: (long)year
  190. {
  191. /*30*/    if((monthIndex==4)||
  192.        (monthIndex==6)||
  193.        (monthIndex==9)||
  194.        (monthIndex==11))    return 30;     
  195. /*31*/    if((monthIndex==1)||
  196.        (monthIndex==3)||
  197.        (monthIndex==5)||
  198.        (monthIndex==7)||
  199.        (monthIndex==8)||
  200.        (monthIndex==12))    return 31;
  201. /*28*/    if((monthIndex==2)&&![self leapYear:year])
  202.                 return 28;
  203. /*29*/    else            return 29;
  204.     return 0;
  205. }
  206.  
  207. - (long) daysInMonth: (long)monthIndex
  208. {
  209.     return [self daysInMonth:monthIndex forYear:[self year]];
  210. }
  211.  
  212. - (long) daysInMonth
  213. {
  214.     return [self daysInMonth:[self month] forYear:[self year]];
  215. }
  216.  
  217. - (long) dayOfMonth
  218. {
  219.     if( timesPtr )
  220.         return timesPtr->tm_mday + 1;
  221.     else
  222.         return 0;
  223. }
  224.  
  225. - (long) dayOfWeek
  226. {
  227.     if( timesPtr )
  228.         return timesPtr->tm_wday + 1;
  229.     else
  230.         return 0;
  231. }
  232.  
  233. - (long) weekOfMonth
  234. {
  235.     if( timesPtr )
  236.         return (timesPtr->tm_mday / 7) + 1;
  237.     return 0;
  238. }
  239.  
  240. - (long) dayOfYear
  241. {
  242.     return [self day];
  243. }
  244.  
  245. - (long) year
  246. {
  247.     if( timesPtr )
  248.         return timesPtr->tm_year + 1900;
  249.     return 0;
  250. }
  251.  
  252. - (long) month
  253. {
  254.     if( timesPtr )
  255.         return timesPtr->tm_mon + 1;
  256.     return 0;
  257. }
  258.  
  259. - (long) week
  260. {
  261.     if( timesPtr )
  262.         return (timesPtr->tm_yday / 7) + 1;
  263.     return 0;
  264. }
  265.  
  266. - (long) day
  267. {
  268.     if( timesPtr )
  269.         return timesPtr->tm_yday + 1;
  270.     return 0;
  271. }
  272.  
  273. - (long) hour
  274. {
  275.     if( timesPtr )
  276.         return timesPtr->tm_hour;
  277.     return 0;
  278. }
  279.  
  280. - (long) minute
  281. {
  282.     if( timesPtr )
  283.         return timesPtr->tm_min;
  284.     return 0;
  285. }
  286.  
  287. - (long) second
  288. {
  289.     if( timesPtr )
  290.         return timesPtr->tm_sec;
  291.     return 0;
  292. }
  293.  
  294. - (long) microSecond
  295. {
  296.     [self notImplemented:_cmd];
  297.     return 0;
  298. }
  299.  
  300. //--------------------------------
  301.  
  302. - _syncTimesStruct
  303. {
  304.     struct tm *tmpTimesPtr;
  305.     
  306.     tmpTimesPtr = localtime( &myTime);
  307.  
  308.     if( !timesPtr )
  309.         timesPtr = NXZoneMalloc( [self zone],
  310.             sizeof( struct tm ) );
  311.     
  312.     if( timesPtr )
  313.         *timesPtr = *tmpTimesPtr;
  314.  
  315.     return self;
  316. }
  317.  
  318. - resetTimeFromTM
  319. {
  320.     myTime = mktime( timesPtr );
  321.     return self;
  322. }
  323.  
  324. - setTime_t:(time_t)num
  325. {
  326.     myTime = num;
  327.     [self _syncTimesStruct];
  328.     return self;
  329. }
  330.  
  331. - setToCurrentTime
  332. {
  333.     struct timezone tzp;
  334.     struct timeval curTime;
  335.  
  336.     if( gettimeofday( &curTime, &tzp) == -1 )
  337.     {
  338.         /* call failed */
  339.         return self;
  340.     }
  341.     [self setTime_t:curTime.tv_sec];
  342.     return self;
  343. }
  344.  
  345. - setTime:(Time *)aTimeObj
  346. {
  347.     if( timesPtr )
  348.     {
  349.         myTime = *[aTimeObj getTime_t];
  350.         [self resetTimeFromTM];
  351.     }
  352.     return self;
  353. }
  354.  
  355. - setYears:(long)num
  356. {
  357.     if( timesPtr )
  358.     {
  359.         timesPtr->tm_year = num - 1900;
  360.         [self resetTimeFromTM];
  361.     }
  362.     return self;
  363. }
  364.  
  365. - setMonths:(long)num
  366. {
  367.     if( timesPtr )
  368.     {
  369.         timesPtr->tm_mon = num - 1;
  370.         [self resetTimeFromTM];
  371.     }
  372.     return self;
  373. }
  374.  
  375. - setWeeks:(long)num
  376. {
  377.     [self setDays:(num-1)*7+[self dayOfWeek]];
  378.     return self;
  379. }
  380.  
  381. /*
  382. ????    tm_yday and tm_wday are ignored with mktime()
  383.     reset mon to 0 then set mday to num
  384. --->    Use the _sync.. method and just use seconds
  385.     (above method don't work?)
  386. */
  387. - setDays:(long)num
  388. {
  389.     if( timesPtr )
  390.     {
  391.         myTime = myTime + (num - [self day])*86400;
  392.         [self _syncTimesStruct];
  393.     }
  394.     return self;
  395. }
  396.  
  397. - setHours:(long)num
  398. {
  399.     if( timesPtr )
  400.     {
  401.         timesPtr->tm_hour = num;
  402.         [self resetTimeFromTM];
  403.     }
  404.     return self;
  405. }
  406.  
  407. - setMinutes:(long)num
  408. {
  409.     if( timesPtr )
  410.     {
  411.         timesPtr->tm_min = num;
  412.         [self resetTimeFromTM];
  413.     }
  414.     return self;
  415. }
  416.  
  417. - setSeconds: (long)numSeconds microSeconds: (long)numMicroSeconds
  418. {
  419.     if( timesPtr )
  420.     {
  421.         timesPtr->tm_sec = numSeconds;
  422.         [self resetTimeFromTM];
  423.     }
  424.     return self;
  425. }
  426.  
  427. - setSeconds: (long)numSeconds
  428. {
  429.     [self setSeconds:numSeconds microSeconds:0];
  430.     return self;
  431. }
  432.  
  433. - setMicroSeconds:(long)num
  434. {
  435.     [self notImplemented:_cmd];
  436.     return self;
  437. }
  438.  
  439. //--------------------------------
  440.  
  441. - addTime: (Time *)aTimeObj
  442. {
  443.     [self setTime_t:*[self getTime_t] + *[aTimeObj getTime_t]];
  444.     return self;
  445. }
  446.  
  447. - addTime_t: (time_t)num
  448. {
  449.     [self setTime_t:*[self getTime_t] + num];
  450.     return self;
  451. }
  452.  
  453. - addYears: (unsigned)num
  454. {
  455.     [self setYears:[self year] + num];
  456.     return self;
  457. }
  458.  
  459. - addMonths: (unsigned)num
  460. {
  461.     [self setMonths:[self month] + num];
  462.     return self;
  463. }
  464.  
  465. - addWeeks: (unsigned)num
  466. {
  467.     [self addDays:num*7];
  468.     return self;
  469. }
  470.  
  471. - addDays: (unsigned)num
  472. {
  473.     [self setDays:[self day] + num];
  474.     return self;
  475. }
  476.  
  477. - addHours: (unsigned)num
  478. {
  479.     [self setHours:[self hour] + num];
  480.     return self;
  481. }
  482.  
  483. - addMinutes: (unsigned)num
  484. {
  485.     [self setMinutes:[self minute] + num];
  486.     return self;
  487. }
  488.  
  489. - addSeconds: (unsigned)num
  490. {
  491.     [self setSeconds:[self second] + num];
  492.     return self;
  493. }
  494.  
  495. - addMicroSeconds: (unsigned)num
  496. {
  497.     [self setMicroSeconds:[self microSecond] + num];
  498.     return self;
  499. }
  500.  
  501. //--------------------------------
  502.  
  503. - subtractTime: (Time *)aTimeObj
  504. {
  505.     [self setTime_t:*[self getTime_t] - *[aTimeObj getTime_t]];
  506.     return self;
  507. }
  508.  
  509. - subtractTime_t: (time_t)num
  510. {
  511.     [self setTime_t:*[self getTime_t] - num];
  512.     return self;
  513. }
  514.  
  515. - subtractYears: (unsigned)num
  516. {
  517.     [self setYears:[self year] - num];
  518.     return self;
  519. }
  520.  
  521. - subtractMonths: (unsigned)num
  522. {
  523.     [self setMonths:[self month] - num];
  524.     return self;
  525. }
  526.  
  527. - subtractWeeks: (unsigned)num
  528. {
  529.     [self subtractDays:num*7];
  530.     return self;
  531. }
  532.  
  533. - subtractDays: (unsigned)num
  534. {
  535.     [self setDays:[self day] - num];
  536.     return self;
  537. }
  538.  
  539. - subtractHours: (unsigned)num
  540. {
  541.     [self setHours:[self hour] - num];
  542.     return self;
  543. }
  544.  
  545. - subtractMinutes: (unsigned)num
  546. {
  547.     [self setMinutes:[self minute] - num];
  548.     return self;
  549. }
  550.  
  551. - subtractSeconds: (unsigned)num
  552. {
  553.     [self setSeconds:[self second] - num];
  554.     return self;
  555. }
  556.  
  557. - subtractMicroSeconds: (unsigned)num
  558. {
  559.     [self setMicroSeconds:[self microSecond] - num];
  560.     return self;
  561. }
  562.  
  563. @end